package com.swm.service;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.swm.dao.UserDao;
import com.swm.jdo.UserDaoImpl;
import com.swm.model.DataManager;
import com.swm.model.User;

@Service	//Spring will create a bean from this class for DI
public class MemberService {
	@Autowired	//Spring will inject this dependency for us
    private UserDao userDao = new UserDaoImpl();
	
	public DataManager getDataManager(){
		return DataManager.getInstance();
	}
	
	public void AddUser( User user){	
		if ( userDao != null ){
			this.userDao.Add(user);			
			getDataManager().addUser(user);
			System.out.println( "[CREATE USER][KEY] = " + user.getKey() );
		}
	}
	
	public void UpdateUser(User user){
		this.userDao.Update(user);
	}
	public void DeleteUser(String userID){
		this.userDao.Delete(userID);
	}
	
	public List<User> getAllUser(){
		return this.userDao.find();
	}
	public User	getUser(String userID){
		return this.userDao.find(userID);
	}
	
	public boolean login( User user ){
		//User member = getUser(  user.getId() );
		User member = getDataManager().getUser( user.getId() );
		if ( member != null ){
			if ( member.isEqual( user ) )
				return true;		
		}
		return false;
	}

}
